﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Backend.DTO;
using Backend.BLL;

namespace ReSeSSApplication
{
    public partial class ResetPassword : System.Web.UI.Page
    {
        UserBLL aUserBll = new UserBLL();
        UserDTO loggedInUserDTO = new UserDTO();
        UserDTO aUser = new UserDTO();

        protected void Page_Load(object sender, EventArgs e)
        {
            AlertSuccess.Visible = false;
            AlertError.Visible = false;

            loggedInUserDTO = (UserDTO)Session["LoggedInUserDTO"];
            RedirectUser(loggedInUserDTO);

            try
            {
                string key = Request.QueryString["key"];

                if (string.IsNullOrEmpty(key))
                {
                    AlertError.InnerText = "Invalid reset key";
                    AlertError.Visible = true;
                    ResetPasswordFormDiv.Visible = false;
                }
                else
                {
                    aUser = aUserBll.GetUserByResetKey(key);
                    if (aUser == null)
                    {
                        AlertError.InnerText = "Invalid reset key";
                        AlertError.Visible = true;
                        ResetPasswordFormDiv.Visible = false;
                    }
                }
            }
            catch (Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
                ResetPasswordFormDiv.Visible = false;
            }

            string m = Request.QueryString["m"];
            if (m == "success")
            {
                AlertError.Visible = false;
                AlertSuccess.InnerHtml = "You have successfully reset your password. <a href='Index.aspx'>Click Here</a> to login to the system.";
                AlertSuccess.Visible = true;
            }
        }

        protected void ResetPasswordButton_Click(object sender, EventArgs e)
        {
            string newPassword = txtNewPassword.Text.Trim();
            string confirmNewPassword = txtConfirmNewPassword.Text.Trim();

            if (string.IsNullOrEmpty(newPassword) || string.IsNullOrEmpty(confirmNewPassword))
            {
                AlertError.InnerText = "Required field can't be empty.";
                AlertError.Visible = true;
            }
            else if (newPassword != confirmNewPassword)
            {
                AlertError.InnerText = "New Password and Confirm New Password does not match";
                AlertError.Visible = true;
            }
            else
            {
                aUserBll.UpdateUser("Password", newPassword, aUser.Id);
                aUserBll.UpdateUser("ResetPasswordKey", string.Empty, aUser.Id);
                Response.Redirect("ResetPassword.aspx?m=success");
            }
        }

        /* Logged in users can't reset password */
        protected void RedirectUser(UserDTO userDTO)
        {
            if (userDTO != null)
            {
                if (userDTO.UserType == "Administrator")
                {
                    Response.Redirect("AdministratorDashboard.aspx");
                }
                else if (userDTO.UserType == "User")
                {
                    Response.Redirect("UserDashboard.aspx");
                }
            }
        }
    }
}